﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Provider;
using DbDoc.BusinessObjects;
using DbDoc.Provider.Persistence;
using DbDoc.Provider.Source;


namespace DbDoc.BusinessManager
{
    public class SettingsManager
    {

        public static ProviderCollection GetPersistenceProviders()
        {
            return PersistenceProviderManager.Providers;
        }

        public static ProviderBase GetSelectedProvider()
        {
            return PersistenceProviderManager.Provider ;
        }        

        public static SourceProvider GetSelectedSourceProvider()
        {
            return SourceProviderManager.Provider;
        }

        public static Setting GetSettingByKey(string key)
        {
            return PersistenceProviderManager.Provider.GetByKey(key);
        }

        public static bool RequireLogin()
        {
            return PersistenceProviderManager.Provider.RequireLogin();
        }

        public static void UserLoginRequired(bool required)
        {
            PersistenceProviderManager.Provider.UserLoginRequired(required);
        }

        /// <summary>
        /// Adds the settings to the db if it is a new one otherwise updates the existing setting
        /// </summary>
        /// <param name="input"></param>
        public static void SaveSetting(Setting input)
        {
            PersistenceProviderManager.Provider.SaveSetting(input);
        }

        public static IList<AppUser> GetAllUsers()
        {
            return PersistenceProviderManager.Provider.GetAllUsers();
        }

        public static void SaveUsers(IList<AppUser> AppUsers)
        {
            // Save/Update user in the database only if Username and Password is specified.
            foreach (AppUser user in AppUsers)
                if(!string.IsNullOrWhiteSpace(user.Username) && !string.IsNullOrWhiteSpace(user.Password))
                    PersistenceProviderManager.Provider.SaveUser(user);
        }
    }
}
